<?

JCAT_Package::ImportClass('JCAT_ViewGtk',JCAT::PACKAGE_MVCGTK) ;

class DataVerifierSettingView extends JCAT_ViewGtk implements JCAT_IViewForm
{
	/**
	 * 控件基本信息
	 * 
	 * @access	public
	 * @param	$aParent=null	JCAT_ViewGtk		拥有此视图的对象
	 * @param	$aUI=null		JCAT_UIGtkGlade		模版引擎
	 * @return	array
	 */
	public function DataVerifierSettingView( JCAT_IViewContainer $aParent, JCAT_UIGtkGlade $aUI=null )
	{
		$this->JCAT_ViewGtk('DataVerifierSettingView','DataVerifierSettingView','DataVerifierSettingView',$aParent,$aUI) ;
		
		
		// 创建 并 初始化 UICtrl ---------------------------

		$aUICtrl = new TreeDataVerifierSetting() ;
		$aUICtrl->RegisterEventProcessFunction('TreeDataVerifierSetting-toggled',array($this,'OnDVEnableToggled')) ;
		$this->AddUICtrl($aUICtrl) ;
	}

	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$aRenderer		GtkCellRendererToggle	what's this
	 * @param	$sPath			string					what's this
	 * @param	$nColumn		int						what's this
	 * @param	$aUICtrl		JCAT_UIGtkCtrlTree		what's this
	 * @return	bool
	 */
	public function OnDVEnableToggled( GtkCellRendererToggle $aRenderer , $sPath, $nColumn, JCAT_UIGtkCtrlTree $aUICtrl )
	{
		$aModel = $aUICtrl->GetGtkModel() ;
		$aItem = $aModel->get_iter($sPath) ;
		JCAT_ASSERT::ASSERT_INSTANCE($aItem,'GtkTreeIter') ;
		
		// DV 文档
		$aDVDoc = $this->GetDVDocFromTreeItem($aModel,$aItem) ;		
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDoc,'IToolboxDataVerifierDoc') ;
		
		// 交由 控制器 处理
		$aController = $this->GetParentController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aController,'DataVerifierSetting') ;
		$aController->ToggledDVEnable($aDVDoc) ;				
		
		return true ;
	}
	
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	array
	 */
	public function OnAddDataVerifer()
	{
		$wndView = $this->GetWidget() ;
		$aWidgetCreator = $wndView->get_data('creator') ;
		JCAT_ASSERT::ASSERT_INSTANCE($aWidgetCreator) ;
		
		$arrObject4Signal = array('TheView'=>$this) ;
		$wndMenu = $aWidgetCreator->GetWidget('MenuAddDataVerifier',&$arrObject4Signal) ;
		
		// 装载 校验器菜单
		if( !$wndMenu->get_data('loaded') )
		{
			// 类注册器
			$theClassRegister = JCAT_Singleton::GetGlobalInstance('ClassRegister') ;
			
			// 控制器
			$aController = $this->GetParentController() ;
			
			// 创建 校验器控制器 的 迭代器
			$aIterator = $theClassRegister->CreateClassesIterator(IToolboxDataVerifierController::FUNCTIONAL_NAME) ;
			$aIterator->First() ;
			while( !$aIterator->IsDone() )
			{
				// 类名
				$sClassName = $aIterator->Current() ;
				
				// 检查有效
				JCAT_ASSERT::ASSERT_IMPLEMENTS($sClassName,'IToolboxDataVerifierController',true,JCAT_Language::Sentence('校验器扩展(类:%s)必须实现 IToolboxDataVerifierController 接口；可能是安装了有故障的扩展，请尝试卸载故障扩展',$sClassName)) ;
				
				// 校验器名称
				$sName = call_user_func(array($sClassName,'GetVerifierName')) ; 
				
				// 创建 菜单选项
				$aMenuItem = new GtkMenuItem($sName) ;
				$wndMenu->append($aMenuItem) ;
				
				// 连接事件
				$aMenuItem->connect_simple('activate',array($aController,'AddDataVerifier'),$sClassName) ;
				
				// 迭代
				$aIterator->Next() ;
			}
			
			$wndMenu->set_data('loaded',true) ;
		}
		
		$wndMenu->set_visible(true) ;
		$wndMenu->popup() ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @return	void
	 */
	public function OnModifyDataVerifer($bRemove=false)
	{
		list($aModel,$aItem) = $this->GetCurrentTreeItem() ;
		if(!$aItem)
		{
			return ;
		}
		
		// DV 文档
		$aDVDoc = $this->GetDVDocFromTreeItem($aModel,$aItem) ;		
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDoc,'IToolboxDataVerifierDoc') ;

		// 交由 控制器 处理
		$aController = $this->GetParentController() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aController,'DataVerifierSetting') ;
		
		if($bRemove)
		{
			$aController->RemoveDataVerifier($aDVDoc) ;		
		}
		else
		{
			$aController->ModifyDataVerifier($aDVDoc) ;					
		}
		
	}	
	
	/**
	 * what's this
	 * 
	 * @access	protected
	 * @return	array, null
	 */
	protected function GetCurrentTreeItem()
	{
		$wndTree = $this->GetChildWidget('TreeDataVerifierSetting') ;

		$wndTree = $this->GetChildWidget('TreeDataVerifierSetting') ;
		JCAT_ASSERT::ASSERT_INSTANCE($wndTree,'GtkTreeView');

		$aSelection = $wndTree->get_selection() ;
		list($aModel,$aItem) = $aSelection->get_selected() ;

		// 用户  未选择
		if( !$aItem )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('请选择数据校验器')) ;
			return array($aModel,null) ;
		}
		
		return array($aModel,$aItem) ;
	}
	
	/**
	 * 鼠标点击事件
	 * 
	 * @access	public
	 * @return	void
	 */
	public function OnButtonPress(GtkWidget $wndTree, GdkEvent $aEvent)
	{
		// echo serialize($aEvent) ;
		if( $aEvent->type == Gdk::_2BUTTON_PRESS )
		{
			$this->OnModifyDataVerifer() ;
		}
	}
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	array
	 */
	public function OnDownDataVerifier()
	{
		echo 'http://dddddooooowwwwwnnnnn' ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	array
	 */
	public function OnDiyDataVerifier()
	{
		echo 'OnDiyDataVerifier' ;
	}
	

	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function LoadDataVerifier()
	{
		$aUICtrlDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aUICtrlDoc,'UICtrlDocBase') ;
		
		$wndTree = $this->GetChildWidget('TreeDataVerifierSetting') ;
		JCAT_ASSERT::ASSERT_INSTANCE($wndTree,'GtkTreeView');
		$aModel = $wndTree->get_model() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aModel,'GtkTreeStore');
		
		// 清空
		$aModel->clear() ;
		
		// 载入
		$aIterator = $aUICtrlDoc->CreateDataVerifierDocIterator() ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$aDVDoc = $aIterator->Current() ;
			
			$sVerifierName = call_user_func( array($aDVDoc->GetControllerName(),'GetVerifierName') ) ;
			$bIsEnble = $aDVDoc->IsEnable() ;
			
			$aItem = $aModel->append(null,array($bIsEnble,$sVerifierName,$aDVDoc->GetRuleIntro(),$aDVDoc->GetDocumentID())) ;
			
			$aIterator->Next() ;
		}
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	private
	 * @return	IToolboxDataVerifierDoc
	 */
	private function GetDVDocFromTreeItem( GtkTreeModel $aModel, GtkTreeIter $aItem )
	{
		// UICtrl 文档
		$aDoc = $this->GetDocument() ;
		JCAT_ASSERT::ASSERT_INSTANCE($aDoc,'UICtrlDocBase') ;
		
		// DV 文档 Index
		$nId = $aModel->get_value($aItem,TreeDataVerifierSetting::CLM_DOCID) ;
		
		// DV 文档
		$aDVDoc = DataVerifierDoc::GetGlobalInsByID($nId) ;		
		JCAT_ASSERT::ASSERT_INSTANCE($aDVDoc,'IToolboxDataVerifierDoc') ;

		return $aDVDoc ;
	}
}

JCAT_Package::ImportInterface('JCAT_IViewForm',JCAT::PACKAGE_MVC) ;

?>